package de.mhu.mandalaui.forms;

import org.codehaus.jackson.node.ObjectNode;

import de.mhu.mandalaui.properties.Property;
import de.mhus.lib.MCast;
import de.mhus.lib.MException;
import de.mhus.lib.MString;
import de.mhus.lib.form.DataConnector;
import de.mhus.lib.form.DataSource;
import de.mhus.lib.form.UiElement;

public abstract class UiMandala extends UiElement {

	protected Property property;
	private String description = null;
	private boolean changed;
	private boolean enabled;
	private String value;
	private String label;
	private String errorMessage;
	
	@Override
	protected void doConnect() {
		
	}

	@Override
	protected void doDisconnect() {
		
	}

	@Override
	public void doUpdate(DataConnector data) throws MException {
		if (data.getTaskName().equals(DataSource.CONNECTOR_TASK_TITLE)) {
			doUpdateLabel(data);
			return;
		}
		if (data.getTaskName().equals(DataSource.CONNECTOR_TASK_ERROR)) {
			String msg = data.getString("");
			setErrorMessage(msg);
			return;
		}
		if (data.getTaskName().equals(DataSource.CONNECTOR_TASK_DATA)) {
			doUpdateData(data);
			return;
		}
		
		if (data.getTaskName().equals(DataSource.CONNECTOR_TASK_ENABLED)) {
			doUpdateEnabled(data);
			return;
		}
				
		if (data.getTaskName().equals(DataSource.CONNECTOR_TASK_DESCRIPTION)) {
			doUpdateDescription(data);
			return;
		}

	}


	protected void doUpdateDescription(DataConnector data) {
		try {
			String value = data.getString(description);
			if (equals(value,description)) return;
			description = value;
			changed = true;
		} catch (MException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	protected void doUpdateEnabled(DataConnector data) {
		try {
			boolean value = data.getBoolean(enabled);
			if (equals(value,description)) return;
			enabled = value;
			changed = true;
		} catch (MException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	protected void doUpdateData(DataConnector data) {
		try {
			String v = data.getString(value);
			if (equals(v,value)) return;
			value = v;
			changed = true;
		} catch (MException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	protected void doUpdateLabel(DataConnector data) {
		try {
			String value = data.getString(label);
			if (equals(value,label)) return;
			label = value;
			changed = true;
		} catch (MException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void setErrorMessage(String value) {
		if (equals(value,errorMessage)) return;
		errorMessage = value;
		changed = true;
	}

	public void createUi(MandalaFormBuilder builder) {
		property = createProperty();
		builder.getCurrentTable().add(property, null);
	}

	protected abstract Property createProperty();

	public void fillArray(ObjectNode node) {
		String name = getElement().getName();
		node.put(name, value);
		node.put(name + "_label", label);
		node.put(name + "_enabled", enabled);
		node.put(name + "_error", errorMessage);
		node.put(name + "_desc", description);

	}

	public boolean isChanged() {
		return changed;
	}

	public void setChanged(boolean changed) {
		this.changed = changed;
	}
	
}
